package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.RegionMapper;
import com.bsh.mybatis.model.Region;
import com.mysql.jdbc.StringUtils;

@Repository
public class DaoRegion {
	@Resource
	SqlSessionTemplate session;
	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;

	RegionMapper getMapper() {
		return session.getMapper(RegionMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(Region record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(Region record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public Region selectByPrimaryKey(Integer id) {
		Region row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(Region record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(Region record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}

	
	public List<Region> getRegionList(int pid) {
		List<Region> res = getMapper().getRegionList(pid);
		return res;
	}

	public List<Region> getRegionAllCity(Integer regionLevel) {
		List<Region> res = getMapper().getRegionAllCity(regionLevel);
		return res;
	}
	
	public List<Map<String, Object>> getSelectedRegion(String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("select * from region  ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		sb.append("  order by py asc");
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);
		return list;
	}
	
	
	
	
}
